﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zy1.Models;

namespace zy1.Controllers
{
    public class DefaultController : Controller
    {
        Model1 model = new Model1();
        public ActionResult Index(int ID = 0, string Name = "")
        {
            var list = model.ProType.ToList();
            list.Insert(0, new ProType() { Name = "全部", ID = 0 });
            ViewBag.ID = new SelectList(list,"ID", "Name");
            var l = model.ProInfo.Where(p=>p.Name.Contains(Name)&&(ID==0)||p.ID==ID).ToList();
            return View(l);
        }
        public ActionResult Create()
        {
            ViewBag.ID = new SelectList(model.ProType, "ID", "Name");
            return View();
        
        }
        [HttpPost]
        public ActionResult Create(ProInfo proInfo)
        {
            if (ModelState.IsValid) {

                proInfo.IsDestroy = false;
                model.ProInfo.Add(proInfo);
                var res = model.SaveChanges();
                if (res > 0) {
                    return RedirectToAction("Index");
                        }
               
            }
            ViewBag.ID = new SelectList(model.ProType, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Delete(int ? id) {

            if (id==null) {
                return Json(false);
            
            }
            var task = model.ProInfo.FirstOrDefault(p => p.ID ==id);
            if (task==null) {
                return Json(false);
            
            }
            task.IsDestroy = true;
            var a = model.SaveChanges();
            return Json(a>0);

        }
    }
}